//
//  UMengManager.m
//  ZPrograme
//
//  Created by 周成龙 on 16/2/20.
//  Copyright © 2016年. All rights reserved.
//

#import "UMengManager.h"

#import "UMSocialQQHandler.h"
#import "UMSocial.h"
#import "UserModel.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"
#import "WXApi.h"

#import "VDJConstant.h"
#import "JQProgressHUD.h"

@implementation UMengManager
+ (instancetype)manager
{
    static UMengManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[UMengManager alloc] init];
    });
    return manager;
}

// QQ登陆
- (void)qqLoginInVC:(UIViewController *)controller
{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    
    snsPlatform.loginClickHandler(controller,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //获取用户名、uid、token等
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
            MyLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            //保存登陆用户信息
            UserModel * userModel = [[UserModel alloc]init];
            userModel.token = snsAccount.accessToken;
            userModel.uid = snsAccount.usid;
            userModel.username = snsAccount.userName;
            userModel.avatar = snsAccount.iconURL;
            [userModel save];
            //页面跳转
        }});
}

// QQ分享
- (void)shareQQWithVC:(UIViewController *)controller
         withShareURL:(NSString *)shareURL
      withUrlResource:(NSString *)urlResource
       withShareTitle:(NSString *)title
        withShareText:(NSString *)shareText
{
    if ([QQApiInterface isQQInstalled]) {
        // 分享弹框的消息类型
        [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
        // 分享弹框的标题
        [UMSocialData defaultData].extConfig.qqData.title = title;
        // 分享弹框的图片 url
        UMSocialUrlResource *resource = [[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeImage url:urlResource];
        // 分享出去的 url
        [UMSocialData defaultData].extConfig.qqData.url = shareURL;
        
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQQ] content:shareText image:nil location:nil urlResource:resource presentedController:controller completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil message:@"分享成功" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil];
                [alertView show];
            } else if(response.responseCode != UMSResponseCodeCancel) {
                [JQProgressHUD showErrorMessage:@"分享失败"];
            }
        }];
    }else
    {
        [JQProgressHUD showErrorMessage:@"手机未安装QQ设备"];
    }
}

// 微信分享
- (void)shareWeiXinWithVC:(UIViewController *)controller
             withShareURL:(NSString *)shareURL
          withUrlResource:(NSString *)urlResource
           withShareTitle:(NSString *)title
            withShareText:(NSString *)shareText;
{
    if ([WXApi isWXAppInstalled]) {
        // 分享弹框的消息类型
        [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
        // 分享弹框的标题
        [UMSocialData defaultData].extConfig.wechatSessionData.title = title;
        // 分享出去的 url
        [UMSocialData defaultData].extConfig.wechatSessionData.url = shareURL;
        
        UMSocialUrlResource *resource = [[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeImage url:urlResource];
        
        [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatSession] content:shareText image:nil location:nil urlResource:resource presentedController:controller completion:^(UMSocialResponseEntity * response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil message:@"分享成功" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil];
                [alertView show];
            } else if(response.responseCode != UMSResponseCodeCancel) {
                [JQProgressHUD showErrorMessage:@"分享失败"];
            }
        }];
    }else
    {
        [JQProgressHUD showErrorMessage:@"手机未安装微信设备"];
    }
}

@end
